package com.hd.pzhwlxt.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hd.pzhwlxt.pojo.Notice;
import com.hd.pzhwlxt.service.NoticeService;
import com.hd.pzhwlxt.utils.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

@RestController
@RequestMapping("/notice")
public class NoticeController {
    @Autowired
    private NoticeService noticeService;

    @GetMapping("/getAll")
    public Response<Page<Notice>> getAllNotice(@RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum, @RequestParam(value = "pageSize",defaultValue = "15") Integer pageSize){
        Page<Notice> notice = noticeService.getAllNotice(pageNum,pageSize);
        if(notice!=null){
            return Response.createSuccessResponse("查询成功", notice);
        }
        return Response.createErrorResponse("查询失败");
    }

    @GetMapping("/get/{id}")
    public Response<Notice> getNoticeById(@PathVariable Integer id){
        Notice notice = noticeService.getById(id);
        if(notice!=null){
            return Response.createSuccessResponse("查询成功",notice);
        }
        return Response.createErrorResponse("查询失败");
    }

    @PostMapping("/add")
    public Response<String> addNoticeInfo(@RequestBody Notice notice){
        System.out.println(notice);
        boolean save = noticeService.save(notice);
        if (save){
            return Response.createSuccessResponse("添加成功");
        }
        return Response.createErrorResponse("添加失败");
    }

    @DeleteMapping("/delmore/{ids}")
    public Response<String> deleteNoticeByIds(@PathVariable Integer[] ids){
        boolean remove = noticeService.removeByIds(Arrays.asList(ids));
        if (remove){
            return Response.createSuccessResponse("删除成功");
        }
        return Response.createErrorResponse("删除失败");
    }

    @PutMapping("/update")
    public Response<String> updateNotice(@RequestBody Notice notice){
        boolean update = noticeService.updateById(notice);
        if (update){
            return Response.createSuccessResponse("修改成功");
        }
        return Response.createErrorResponse("修改失败");
    }
}
